<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/19
 * Time: 15:47
 */

namespace app\mall\controller;




use app\admin\model\HomeAdvertisement;
use app\mall\model\GoodsSpike;

class Spike extends Base
{


    //秒杀页面广告
    public function spikeAdvertisement(){
        $data = HomeAdvertisement::where('id',4)->find();
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        $data['image'] = $this->url.$data['image'];
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data]);
    }


    //秒杀商品列表
    public function spikeList(){
        $page = (int)input('post.page');
        $limit = (int)input('post.limit');
        if ($page == 0){
            return json(['code'=>-1,'msg'=>'参数page不能为空']);
        }
        if ($limit == 0){
            return json(['code'=>-1,'msg'=>'参数limit不能为空']);
        }
        $data = GoodsSpike::order('sort desc')
            ->field('id,spike_title,
            spike_image,spike_price,original_price,
            stock,sell,start_time,end_time,status')
            ->page($page,$limit)
            ->select()->each(function ($item){
                $item->spike_image = $this->url.$item->spike_image;
                $item->status = [
                    'status'=>$item->status,
                    'status_text'=>$item->status_text,
                ];
            })->toArray();
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        foreach ($data as $k=>$v){
            $data[$k]['than'] = round(($v['sell']/$v['stock'])*100,2);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data]);
    }


    //首页限时秒杀
    public function homeSpike(){
        $data = GoodsSpike::where('type',2)->order('sort desc')
            ->field('id,spike_title,spike_image,spike_price,original_price,
            stock,sell,start_time,end_time,status')
            ->select()->each(function ($item){
                $item->spike_image = $this->url.$item->spike_image;
                $item->status = [
                    'status'=>$item->status,
                    'status_text'=>$item->status_text,
                ];
            });
        if (!$data){
            return json(['code'=>0,'msg'=>'无数据']);
        }
        foreach ($data as $k=>$v){
            $data[$k]['than'] = round(($v['sell']/$v['stock'])*100,2);
        }
        return json(['code'=>1,'msg'=>'返回成功','data'=>$data]);
    }


}